var $;
var layer;
var table;
$(function() {
    layui.use(['table', 'layer'], function () {
        table = layui.table;
        $=layui.jquery;
        layer=layui.layer;

        //获取用户信息
            $.post("/login/selUserContent", {}, function (res) {
                if (res.code>-1) {
                    getlog(res.obj.userId);
                } else {
                    layer.msg("数据加载失败", {icon: 2});
                }
            });

        //查询登录日志
        function getlog(id){
            $.get("/login/selUserLog", {"uId":id}, function (data) {
                if (data.code>-1) {
                    for (var i = 0; i < data.obj.length; i++){
                        var datetime=settime(data.obj[i].loginDate);
                        data.obj[i].loginDate=datetime;
                    }
                    getTab(data.obj);
                } else {
                    layer.msg("数据加载失败", {icon: 2});
                }
            });
        };

        function getTab(data) {
            table.render({
                elem: '#tabId',
                data: data
                , title: '登录日志表'
                , page: true
                , limit: 10
                , cols: [[
                    {field: 'loginDate', title: '登录时间', width: 276},
                    {field: 'loginIp', title: "登录IP", width: 276},
                    {field: 'loginLocation', title: '登录地区', width: 276}
                ]]
            });
        };

        /*日期转换格式*/
        function settime(da){
            var date=new Date(da);
            var year=date.getFullYear();
            var month=date.getMonth()+1;
            var day=date.getDate();
            var hours=date.getHours();
            var mm=date.getMinutes();
            var s=date.getSeconds();
            return year+'-'+add(month)+'-'+add(day)+' '+add(hours)+':'+add(mm)+':'+add(s);
            //return year+'-'+month+'-'+day+' '+hours+':'+mm+':'+s;
        };
        function add(time){//给非整数添0
            return time<10?'0'+time:time;
        };
    });
});